package cn.gcyl.module.biz.service.useragreement;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.useragreement.vo.*;
import cn.gcyl.module.biz.dal.dataobject.useragreement.UserAgreementDO;
import cn.gcyl.module.biz.dal.mysql.useragreement.UserAgreementMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.USER_AGREEMENT_NOT_EXISTS;

/**
 * 用户协议 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserAgreementServiceImpl implements UserAgreementService {

    @Resource
    private UserAgreementMapper userAgreementMapper;

    @Override
    public Long createUserAgreement(UserAgreementSaveReqVO createReqVO) {
        // 插入
        UserAgreementDO userAgreement = BeanUtils.toBean(createReqVO, UserAgreementDO.class);
        userAgreementMapper.insert(userAgreement);

        // 返回
        return userAgreement.getId();
    }

    @Override
    public void updateUserAgreement(UserAgreementSaveReqVO updateReqVO) {
        // 校验存在
        validateUserAgreementExists(updateReqVO.getId());
        // 更新
        UserAgreementDO updateObj = BeanUtils.toBean(updateReqVO, UserAgreementDO.class);
        userAgreementMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserAgreement(Long id) {
        // 校验存在
        validateUserAgreementExists(id);
        // 删除
        userAgreementMapper.deleteById(id);
    }

    @Override
        public void deleteUserAgreementListByIds(List<Long> ids) {
        // 删除
        userAgreementMapper.deleteByIds(ids);
        }


    private void validateUserAgreementExists(Long id) {
        if (userAgreementMapper.selectById(id) == null) {
            throw exception(USER_AGREEMENT_NOT_EXISTS);
        }
    }

    @Override
    public UserAgreementDO getUserAgreement(Long id) {
        return userAgreementMapper.selectById(id);
    }

    @Override
    public PageResult<UserAgreementDO> getUserAgreementPage(UserAgreementPageReqVO pageReqVO) {
        return userAgreementMapper.selectPage(pageReqVO);
    }

}